package com.mesmileAI.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mesmileAI.entity.SmileText;
import com.mesmileAI.mapper.SmileTextMapper;
import com.mesmileAI.service.SmileTextService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
* @author Administrator
* @description 针对表【smile_text】的数据库操作Service实现
* @createDate 2023-10-28 09:32:29
*/
@Service
public class SmileTextServiceImpl extends ServiceImpl<SmileTextMapper, SmileText> implements SmileTextService {

    @Autowired
    SmileTextMapper smileTextMapper;



    @Override
    public List<SmileText> getChild(SmileText parent,List<SmileText>smileTexts){
        //去除父节点，并且排序
        return smileTexts.stream().filter(i -> Objects.equals(i.getIndexId(), parent.getId())).peek(
                (i) -> i.setChildren(getChild(i, smileTexts))).toList();

    }

    /**
     * tree
     * @param smileTexts
     * @return
     */
    @Override
    public List<SmileText> getChildByOrder(List<SmileText> smileTexts) {
        //找到最大的层级
        Integer hierarchy = smileTexts.stream().max(Comparator.comparing(SmileText::getHierarchy)).get().getHierarchy();
        for (int i=hierarchy;i>1;i--){
            QueryWrapper<SmileText> queryWrapper = new QueryWrapper<>();
            List<SmileText> list = smileTextMapper.selectList(queryWrapper.lambda().eq(SmileText::getHierarchy, i));
            List<SmileText> collect = new ArrayList<>();
            //去重
            collect =list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SmileText::getIndexId))), ArrayList::new));
            for (SmileText smileText : collect){
                //找到上级，如果层级是父级的话找不到
                List<SmileText> list1 = smileTexts.stream().filter(j -> Objects.equals(j.getId(), smileText.getIndexId())).toList();
                //如果父级找不到的话就直接删除子集
                if (list1.isEmpty()){
                    smileTexts.remove(smileText);
                }
                else{
                //找到同类
                List<SmileText> order = new ArrayList<>(smileTexts.stream().filter(smileText1 -> Objects.equals(smileText1.getIndexId(), smileText.getIndexId())).toList());
                //排序
                List<SmileText> orderEd = order.stream().sorted(Comparator.comparing(SmileText::getLevel)).toList();
                //塞值
                list1.get(0).setChildren(orderEd);
                //删除该元素
                smileTexts.removeAll(orderEd);
                }
            }

        }
        return smileTexts;
    }
    //查询深度
    @Override
    public Integer getDeep(SmileText smileText) {
        int i=0;

        do {
            smileText=smileTextMapper.selectById(smileText.getIndexId());
            i++;
        }while ((!Objects.equals(smileText,null)));
        //返回深度
        return i;
    }


    //插值
    @Override
    public List<SmileText> setSmileText(List<SmileText> list, SmileText smileText, int number) {
        //自肃
        list.remove(smileText);
        List<SmileText> list1 = new ArrayList<>(list.stream().sorted(Comparator.comparing(SmileText::getLevel)).toList());
        int mnumber=1;
        for (SmileText j : list1) {
            j.setLevel(mnumber);
            mnumber++;
        }
        if (list1.size()<number){
            smileText.setLevel(number);
            list.add(smileText);
            return list;
        }
        //插值
        SmileText smileText1 = list1.get(number - 1);
        //截取该部分以后的部分
        List<SmileText> list2 = list1.stream().filter(i -> i.getLevel() >= smileText1.getLevel()).toList();
        list1.removeAll(list2);
        //处理
        smileText.setLevel(smileText1.getLevel());
        //自加一
        list2.forEach(s->s.setLevel(s.getLevel()+1));
        //删除添加
        list1.add(smileText);
        list1.addAll(list2);
        return list1;
    }

    @Override
    public Integer getNumber(List<SmileText> list, SmileText smileText) {
        //自肃
        List<SmileText> list1 = new ArrayList<>(list.stream().sorted(Comparator.comparing(SmileText::getLevel)).toList());
        int mnumber=1;
        for (SmileText j : list1) {
            j.setLevel(mnumber);
            mnumber++;
        }
        int count = 0;
        for (SmileText smileText1:list1){
            if (Objects.equals(smileText1.getId(),smileText.getId())){
                count=smileText1.getLevel();
                break;
            }
        }
        return count;
    }
}




